﻿using JiaBoPrint.Model;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Media;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using System.Windows.Forms;

namespace JiaBoPrint
{
    public partial class 沙驰商品尺寸打印 : Form
    {
        private IntPtr Gp_IntPtr;                   //驱动打印句柄
        private libUsbContorl.UsbOperation NewUsb = new libUsbContorl.UsbOperation();
        public 沙驰商品尺寸打印()
        {
            InitializeComponent();
        }
        private void SendData2USB(byte[] str)
        {
            NewUsb.SendData2USB(str, str.Length);
        }
        private void SendData2USB(string str)
        {
            byte[] by_SendData = System.Text.Encoding.GetEncoding(54936).GetBytes(str);
            SendData2USB(by_SendData);
        }
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)//如果输入的是回车键  
            {
                string dm = textBox1.Text.ToString();
                StringBuilder str = new StringBuilder();
                str.Append("{\"skc\":\"" + dm + "\"}");
                string serJson = str.ToString();
                //http://api.satchi.com.cn/SatchiApi/GetJiaBoPrint/GetProductSize?skc=88PMB8245002001
                //http://localhost:30301/SatchiApi/GetJiaBoPrint/GetProductSize?skc=88PMB8245002001
                string json = Helper.HttpGet_query("http://api.satchi.com.cn/SatchiApi/GetJiaBoPrint/GetProductSize?skc=" + dm + "");
                ProductSize persons = JsonConvert.DeserializeObject<ProductSize>(json);//用List作为容器装Person数组

                int code = persons.code ;
                string message = persons.message;
                //code-1未查到，0有数据
                if (code == -1)
                {
                    string micPath = Application.StartupPath;
                    SoundPlayer player = new SoundPlayer(micPath + @"\callvoice.wav");
                    player.Play();
                    frmError frmerror = new frmError();
                    frmerror.ShowDialog();
                }
                else
                {
                    string micPath = Application.StartupPath;
                    SoundPlayer player = new SoundPlayer(micPath + @"\ok.wav");
                    player.Play();

                    List<ListSize> lissize = persons.list;

                    for (int i = 0; i < lissize.Count; i++)
                    {
                        NewUsb.FindUSBPrinter();
                        for (int k = 0; k < NewUsb.USBPortCount; k++)
                        {
                            if (NewUsb.LinkUSB(k))
                            {
                                SendData2USB("SIZE 60 mm,80 mm\r\n");//设置标签大小，单位mm.具体参数请用尺子量一下
                                SendData2USB("GAP 2 mm,0 mm\r\n");//设置两个标签之间的间隙，单位mm.具体参数请用尺子量一下
                                SendData2USB("CLS\r\n");//清空缓冲区
                                SendData2USB("DENSITY 7\r\n");//打印浓度
                                SendData2USB("REFERENCE 0,0\r\n");

                                SendData2USB("TEXT 10,10,\"TSS24.BF2\",0,1,1,\"款号:\"\r\n");
                                SendData2USB("TEXT 120,10,\"TSS24.BF2\",0,1,1,\"" + lissize[i].kh.ToString() + "\"\r\n");

                                SendData2USB("TEXT 10,50,\"TSS24.BF2\",0,1,1,\"品名:\"\r\n");
                                SendData2USB("TEXT 120,50,\"TSS24.BF2\",0,1,1,\"" + lissize[i].pm.ToString() + "\"\r\n");

                                SendData2USB("TEXT 10,90,\"TSS24.BF2\",0,1,1,\"工厂:\"\r\n");
                                SendData2USB("TEXT 120,90,\"TSS24.BF2\",0,1,1,\"" + lissize[i].gc.ToString() + "\"\r\n");

                                string type = lissize[i].type.ToString();
                                if (type == "上衣")
                                {
                                    SendData2USB("TEXT 10,130,\"TSS24.BF2\",0,1,1,\"前/后长:\"\r\n");
                                    SendData2USB("TEXT 160,130,\"TSS24.BF2\",0,1,1,\"" + lissize[i].sy_qianhouchang.ToString() + "\"\r\n");

                                    SendData2USB("TEXT 10,170,\"TSS24.BF2\",0,1,1,\"肩宽:\"\r\n");
                                    SendData2USB("TEXT 120,170,\"TSS24.BF2\",0,1,1,\"" + lissize[i].sy_jiankuan.ToString() + "\"\r\n");

                                    SendData2USB("TEXT 10,210,\"TSS24.BF2\",0,1,1,\"胸围:\"\r\n");
                                    SendData2USB("TEXT 120,210,\"TSS24.BF2\",0,1,1,\"" + lissize[i].sy_xiongwei.ToString() + "\"\r\n");

                                    SendData2USB("TEXT 10,250,\"TSS24.BF2\",0,1,1,\"腰围:\"\r\n");
                                    SendData2USB("TEXT 120,250,\"TSS24.BF2\",0,1,1,\"" + lissize[i].sy_yaowei.ToString() + "\"\r\n");

                                    SendData2USB("TEXT 10,290,\"TSS24.BF2\",0,1,1,\"摆围:\"\r\n");
                                    SendData2USB("TEXT 120,290,\"TSS24.BF2\",0,1,1,\"" + lissize[i].sy_baiwei.ToString() + "\"\r\n");

                                    SendData2USB("TEXT 10,330,\"TSS24.BF2\",0,1,1,\"袖长:\"\r\n");
                                    SendData2USB("TEXT 120,330,\"TSS24.BF2\",0,1,1,\"" + lissize[i].sy_xiuchang.ToString() + "\"\r\n");

                                    SendData2USB("TEXT 10,370,\"TSS24.BF2\",0,1,1,\"袖肥:\"\r\n");
                                    SendData2USB("TEXT 120,370,\"TSS24.BF2\",0,1,1,\"" + lissize[i].sy_xiufei.ToString() + "\"\r\n");

                                    SendData2USB("TEXT 10,410,\"TSS24.BF2\",0,1,1,\"夹圈:\"\r\n");
                                    SendData2USB("TEXT 120,410,\"TSS24.BF2\",0,1,1,\"" + lissize[i].sy_jiaquan.ToString() + "\"\r\n");

                                    SendData2USB("TEXT 10,450,\"TSS24.BF2\",0,1,1,\"袖口:\"\r\n");
                                    SendData2USB("TEXT 120,450,\"TSS24.BF2\",0,1,1,\"" + lissize[i].sy_xiukou.ToString() + "\"\r\n");

                                    SendData2USB("TEXT 10,490,\"TSS24.BF2\",0,1,1,\"领围:\"\r\n");
                                    SendData2USB("TEXT 120,490,\"TSS24.BF2\",0,1,1,\"" + lissize[i].sy_lingwei.ToString() + "\"\r\n");
                                }
                                else if (type == "裤子")
                                {
                                    SendData2USB("TEXT 10,130,\"TSS24.BF2\",0,1,1,\"裤长:\"\r\n");
                                    SendData2USB("TEXT 120,130,\"TSS24.BF2\",0,1,1,\"" + lissize[i].kz_kuchang.ToString() + "\"\r\n");

                                    SendData2USB("TEXT 10,170,\"TSS24.BF2\",0,1,1,\"腰围:\"\r\n");
                                    SendData2USB("TEXT 120,170,\"TSS24.BF2\",0,1,1,\"" + lissize[i].kz_yaowei.ToString() + "\"\r\n");

                                    SendData2USB("TEXT 10,210,\"TSS24.BF2\",0,1,1,\"臀围:\"\r\n");
                                    SendData2USB("TEXT 120,210,\"TSS24.BF2\",0,1,1,\"" + lissize[i].kz_tuwei.ToString() + "\"\r\n");

                                    SendData2USB("TEXT 10,250,\"TSS24.BF2\",0,1,1,\"横档:\"\r\n");
                                    SendData2USB("TEXT 120,250,\"TSS24.BF2\",0,1,1,\"" + lissize[i].kz_hengdang.ToString() + "\"\r\n");

                                    SendData2USB("TEXT 10,290,\"TSS24.BF2\",0,1,1,\"膝围:\"\r\n");
                                    SendData2USB("TEXT 120,290,\"TSS24.BF2\",0,1,1,\"" + lissize[i].kz_qiwei.ToString() + "\"\r\n");

                                    SendData2USB("TEXT 10,330,\"TSS24.BF2\",0,1,1,\"脚口:\"\r\n");
                                    SendData2USB("TEXT 120,330,\"TSS24.BF2\",0,1,1,\"" + lissize[i].kz_jiaokou.ToString() + "\"\r\n");

                                    SendData2USB("TEXT 10,370,\"TSS24.BF2\",0,1,1,\"前浪:\"\r\n");
                                    SendData2USB("TEXT 120,370,\"TSS24.BF2\",0,1,1,\"" + lissize[i].kz_qianlang.ToString() + "\"\r\n");

                                    SendData2USB("TEXT 10,410,\"TSS24.BF2\",0,1,1,\"后浪:\"\r\n");
                                    SendData2USB("TEXT 120,410,\"TSS24.BF2\",0,1,1,\"" + lissize[i].kz_houlang.ToString() + "\"\r\n");

                                    SendData2USB("TEXT 10,450,\"TSS24.BF2\",0,1,1,\"钮牌:\"\r\n");
                                    SendData2USB("TEXT 120,450,\"TSS24.BF2\",0,1,1,\"" + lissize[i].kz_niupai.ToString() + "\"\r\n");

                                }

                                SendData2USB("PRINT 1\r\n");
                                NewUsb.CloseUSBPort();

                                textBox1.Text = "";
                            }
                            else
                            {
                                MessageBox.Show("请确定是否连接上打印机");
                            }
                        }
                    }
                }
            }
        }
    }
}
